
import 'package:flutter/material.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_number.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_widget.dart';


/*
指示器: 条形, 圆形


第三方:
https://pub-web.flutter-io.cn/packages/percent_indicator

https://www.jianshu.com/p/9ac2d0fd4653

 */
abstract class XWidgetIndicatorUtils{

  //原生进度条///////////////////////////////////////////

  //条形进度条
  static Widget getWidgetLinearProgressIndicator(
      {
        required double currentValue,//0~1之间
        Color? backgroundColor= const Color(0xFFFAFAFA),// 进度条背景颜色
        Color? color,//进度条颜色(不渐变)
        Color? startColor,//渐变颜色
        Color? endColor,//渐变颜色
        double? width,
        double height=4,
        EdgeInsets? margin,//getEdgeInsets(left,top,right,bottom)
        EdgeInsets? padding,//getEdgeInsets(left,top,right,bottom)
        Color? borderColor,//边框颜色
        double borderWidth=0,
        double radius_topLeft=2,
        double radius_topRight=2,
        double radius_bottomLeft=2,
        double radius_bottomRight=2
      }
      ){

    double radius = height/2;

    if(currentValue<0||currentValue>1){
      currentValue=0.1;
    }

    //是否使用渐变色
    bool isUseStartEndColor=false;
    if(startColor!=null && endColor!=null){
      isUseStartEndColor=true;
      color=null;
    }

    //
    Widget itemWidget;
    //
    if(isUseStartEndColor){
      //使用渐变
      int percent=XNumberUtils.d2int_floor(currentValue*100);
      //
      itemWidget=Row(
        children: [
          Flexible(
            flex: percent,
            fit: FlexFit.tight,
            child: Container(
              decoration: BoxDecoration(
                gradient: LinearGradient(
                  colors: [startColor!, endColor!], // 设置渐变颜色，可以添加更多颜色
                  begin: Alignment.centerLeft,
                  end: Alignment.centerRight,
                ),
                borderRadius:
                //percent == 100? BorderRadius.all(Radius.circular(radius)): BorderRadius.all(Radius.circular(radius)),
                BorderRadius.only(
                  topLeft: Radius.circular(radius_topLeft),
                  topRight: Radius.circular(radius_topRight),
                  bottomLeft: Radius.circular(radius_bottomLeft),
                  bottomRight: Radius.circular(radius_bottomRight),
                ),
              ),
              child:  SizedBox(height: height),
            ),
          ),
          Flexible(
            fit: FlexFit.tight,
            flex: 100 - percent,
            child: Container(
              decoration: BoxDecoration(
                color: Colors.transparent,//backgroundColor,
                borderRadius: percent == 0
                    ?  BorderRadius.all(Radius.circular(radius))
                    :  BorderRadius.only(
                    bottomRight: Radius.circular(radius),
                    topRight: Radius.circular(radius)),
              ),
              child:  SizedBox(height: height),
            ),
          ),
        ],
      );

    }else{
      //不使用渐变
      itemWidget=LinearProgressIndicator(
        backgroundColor: backgroundColor,
        color: color,
        minHeight: height, // 进度条宽度
        valueColor: AlwaysStoppedAnimation<Color>(color!),
        value: currentValue, // 当前进度（0-1之间）
        //semanticsLabel:semanticsLabel,指定屏幕阅读器应该读取的文本
        //semanticsValue: semanticsValue,指定屏幕阅读器应该读取的文本
      );
    }


    return XWidgetUtils.getXLayoutRoundedRectWithBorder(
      child:itemWidget,
      width: width,
      height: height,
      margin: margin,
      padding: padding,
      backgroundColor: backgroundColor,
      borderColor: borderColor,
      borderWidth: borderWidth,
      radius_topLeft: radius_topLeft,
      radius_topRight: radius_topRight,
      radius_bottomLeft: radius_bottomLeft,
      radius_bottomRight: radius_bottomRight,
    );

  }


  //圆形进度条
  static Widget getWidgetCircularProgressIndicator(
      {
        required double currentValue,//0~1之间
        Color? backgroundColor,// 进度条背景颜色
        Color? color,//进度条颜色
        double width=4,
      }
      ){

    if(currentValue<0||currentValue>1){
      currentValue=0.2;
    }

    return CircularProgressIndicator(
      backgroundColor: backgroundColor,
      color: color,
      strokeWidth: width, // 进度条宽度
      valueColor: color != null?AlwaysStoppedAnimation<Color>(color):null,
      value: currentValue, // 当前进度（0-1之间）
      //semanticsLabel:semanticsLabel,指定屏幕阅读器应该读取的文本
      //semanticsValue: semanticsValue,指定屏幕阅读器应该读取的文本
    );

  }

}
